package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.junzhiit.nengji.dao.FunctionDAO;
import com.junzhiit.nengji.model.Function;

@Repository
public class FunctionDAOImpl extends AbstractBaseDAO<Function> implements FunctionDAO {

	@Override
	public List<Function> findByPlatform(int platform) {
		StringBuilder sql = new StringBuilder();
		sql.append("select * from function where platform = ? order by level,sort");
		StatementParameter params = new StatementParameter();
		params.setInt(platform);
		return super.queryForList(sql.toString(), Function.class, params);
	}

	@Override
	public int updateSort(String parentId, int startIndex, int endIndex, boolean isDesc) {
		StringBuilder sql = new StringBuilder();
		StatementParameter params = new StatementParameter();
		sql.append("update function set");
		if (isDesc)
			sql.append(" sort = sort - 1");
		else
			sql.append(" sort = sort + 1");
		sql.append(" where parent_id = ? and sort between ? and ?");
		params.setString(parentId);
		params.setInt(startIndex);
		params.setInt(endIndex);
		return super.updateForRecord(sql.toString(), params);
	}

	@Override
	public int deleteByParentId(String parentId) {
		StringBuilder sql = new StringBuilder();
		StatementParameter params = new StatementParameter();
		sql.append("delete from `function` where `parent_id` = ?");
		params.setString(parentId);
		return super.updateForRecord(sql.toString(), params);
	}

}
